package com.cynteka.cyfoman.ui;

import com.cynteka.cyfoman.core.config.*;
import com.cynteka.cyfoman.core.ValueObject;

import javax.swing.*;
import java.awt.event.*;
import java.util.List;

public class ActionParamDlg extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JPanel panel;
    private JPanel mainPanel;
    private JPanel titlePanel;
    private JLabel lTitle;
    private ValueObject valueObject;
    private ActionMD action;

    public ActionParamDlg(ActionMD action, ValueObject valueObject) {
        this();
        this.action = action;
        setTitle(action.getName());
        lTitle.setText("Введите параметры");
        List<ParamMD> params = action.getActionParams();
        this.valueObject = valueObject;
        UIBuilder paramBuilder = (UIBuilder) Context.getBeanFactory().getBean("paramPanelBuilder");
        paramBuilder.createContentPane(mainPanel, params, valueObject);

        valueObject.addListener(new ValueObjectListener() {
            @Override
            public void changed(ValueObject sender) {
                checkConstraint();
            }

            @Override
            public void prepareToBeStored(ValueObject valueObject) {
            }
        });
        checkConstraint();
    }

    private ActionParamDlg() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (checkConstraint()) {
                    onOK();
                }
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

// call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

// call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    protected boolean checkConstraint() {
        for (ParamMD param : action.getActionParams()) {
            if (param.isRequired()) {
                Object value = valueObject.getParam(param);
                if (value == null || String.valueOf(value).trim().length() == 0) {
                    setState(false);
                    return false;
                }
            }
            if (param.getParamType() == ParamType.NUMBER) {
                Object value = valueObject.getParam(param);
                if (value != null && String.valueOf(value).trim().length() > 0) {
                    String s = String.valueOf(value);
                    try {
                        Double.parseDouble(s);
                    } catch (NumberFormatException nfe) {
                        setState(false);
                        return false;
                    }
                }
            }
        }
        setState(true);
        return true;
    }

    protected void setState(boolean state) {
        buttonOK.setEnabled(state);
    }

    protected void onOK() {
        dispose();
    }

    private void onCancel() {
// add your code here if necessary
        dispose();
    }

    public static void main(String[] args) {
        ActionParamDlg dialog = new ActionParamDlg();
        dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }
}
